#!/usr/bin/python


import sys
from PyQt4 import QtGui, Qt, QtCore
import dbus, commands, pyosd, time

class wifiblueShow(QtGui.QWidget):
	def __init__(self, parent=None):
		QtGui.QWidget.__init__(self, parent)
		
		#self.setAttribute(Qt.Qt.WA_NoSystemBackground,True)
		#self.setAutoFillBackground(True)
		
		self.counter = 0
		self.imageName = "w_on_b_on.png"
		self.changeFlag = False
		
		self.keycheck = 0
		
		#self.trayIcon = None
		#self.iconize()		
		
		self.bus = dbus.SystemBus()
		
		COLOR = "#e125d6"
		FONT = "-*-helvetica-*-r-normal--34-*-*-*-*-*-*-*"
		self.p = pyosd.osd(font=FONT, colour=COLOR)
		self.p.set_pos(pyosd.POS_BOT)
		self.p.set_align(pyosd.ALIGN_RIGHT)
		self.p.set_vertical_offset(10)
		
		self.wirelessStatus = self.checkWirelessStatus()
		self.bluetoothStatus = self.checkBluetoothStatus()
		
		if self.wirelessStatus and self.bluetoothStatus:
			self.counter = 0
			self.imageName = "w_on_b_on.png"
		if not self.wirelessStatus and self.bluetoothStatus:
			#print "girdi"
			self.counter = 1
			self.imageName = "w_off_b_on.png"
		if self.wirelessStatus and not self.bluetoothStatus:
			self.counter = 2
			self.imageName = "w_on_b_off.png"
		if not self.wirelessStatus and not self.bluetoothStatus:
			self.counter = 3
			self.imageName = "w_off_b_off.png"
		
        
		pixmap = QtGui.QPixmap(self.imageName)
	        width = pixmap.width()
	        height = pixmap.height()

		#self.setWindowTitle('Status')
	        self.resize(width, height)
	        
	        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
	       
	        
	        self.label = QtGui.QLabel(self)
        	self.label.setPixmap(QtGui.QPixmap(self.imageName))
        	self.setMask(pixmap.mask())
		        
	        #self.update()
	        self.showCenter()
	        
	#def paintEvent(self,event):
		#self.setAttribute(Qt.Qt.WA_NoSystemBackground)
	        
	def keyPressEvent(self, event):
	
		print self.counter
		try:
			self.timer.stop()
		except:
			pass
		if event.key() == QtCore.Qt.Key_Escape:
			self.counter = 0
			self.close()
		if event.key() == QtCore.Qt.Key_Shift:
			self.keycheck = 1
			#print self.keycheck
		if  event.key() == 76 and self.keycheck == 1:
			#print event.key()
			self.counter += 1
			self.changeFlag = True
			self.changePic()
			
	def keyReleaseEvent(self,event):
		if event.key() == QtCore.Qt.Key_Shift and self.changeFlag:
			self.keycheck = 0
			self.timer = QtCore.QTimer()
			self.connect(self.timer, QtCore.SIGNAL("timeout()"), self.doAction)
			self.timer.setSingleShot(True)
			self.timer.start(3500)
			#self.doAction()
			
			
	def doAction(self):
		#print "girdi"
		#time.sleep(3)
		if self.counter % 4 == 0:
			status1 = commands.getstatusoutput("dbus-send --system --type=method_call --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.setWirelessEnabled boolean:true")
			status0 = commands.getstatusoutput("sudo ifconfig wlan3 up")
			status2 = commands.getstatusoutput("sudo hciconfig hci0 up")
			if status1[0] == 0 and status2[0] == 0 and status0[0] == 0:
				self.p.display("wireles is on bluetooth is on")

		elif self.counter % 4 == 1:
			status1 = commands.getstatusoutput("dbus-send --system --type=method_call --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.setWirelessEnabled boolean:false")
			status2 = commands.getstatusoutput("sudo hciconfig hci0 up")
			if status1[0] == 0 and status2[0] == 0:
				self.p.display("wireles is off bluetooth is on")

		elif self.counter % 4 == 2:
			status1 = commands.getstatusoutput("dbus-send --system --type=method_call --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.setWirelessEnabled boolean:true")
			status0 = commands.getstatusoutput("sudo ifconfig wlan3 up")
			status2 = commands.getstatusoutput("sudo hciconfig hci0 down")
			if status1[0] == 0 and status2[0] == 0 and status0[0] == 0:
				self.p.display("wireles is on bluetooth is off")

		elif self.counter % 4 == 3:
			status1 = commands.getstatusoutput("dbus-send --system --type=method_call --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.setWirelessEnabled boolean:false")
			status2 = commands.getstatusoutput("sudo hciconfig hci0 down")
			if status1[0] == 0 and status2[0] == 0:
				self.p.display("wireles is off bluetooth is off")
		else:
			pass
			
		try:
			self.timer.stop()
		except:
			pass
			
		#self.close()
	
	def changePic(self):
	
		if self.counter % 4 == 0:
			self.imageName = "w_on_b_on.png"
				
			#self.iconize("icon/vestel-b-on-w-on.jpg")
			
		elif self.counter % 4 == 1:
			self.imageName = "w_off_b_on.png"

			#self.iconize("icon/vestel-b-off-w-on.jpg")
			
		elif self.counter % 4 == 2:
			self.imageName = "w_on_b_off.png"
			
			#self.iconize("icon/vestel-b-on-w-off.jpg")
			
		elif self.counter % 4 == 3:
			self.imageName = "w_off_b_off.png"
			
			#self.iconize("icon/vestel-b-off-w-off.jpg")

		else:
			pass
		
		self.label.clear()
		self.label.setPixmap(QtGui.QPixmap(self.imageName))
		pixmap = QtGui.QPixmap(self.imageName)
		self.setMask(pixmap.mask())
		self.update()
	
	def checkWirelessStatus(self):
		wlan3 = self.bus.get_object('org.freedesktop.NetworkManager','/org/freedesktop/NetworkManager/Devices/wlan3')
		status = wlan3.getProperties(dbus_interface='org.freedesktop.NetworkManager.Devices')[4]
		if status: return True
		else: return False
		
	def checkBluetoothStatus(self):
		status = commands.getstatusoutput("hciconfig")[1]
		if "UP RUNNING" in status: return True
		else: return False
					
		
	def showCenter(self):
	
	        screen = QtGui.QDesktopWidget().screenGeometry()
	        size =  self.geometry()
	        self.move((screen.width()-size.width())/2, (screen.height()-size.height()))
	        
	def iconize(self,imageName="icon/vestel-b-on-w-on"):
		pixmap = QtGui.QPixmap(imageName)
		icon = QtGui.QIcon(pixmap)
		self.trayIcon = QtGui.QSystemTrayIcon(icon)

if __name__ == "__main__":

	app = QtGui.QApplication(sys.argv)
	osd = wifiblueShow()
	
	#splash=QtGui.QSplashScreen(QtGui.QPixmap("w_off_b_on.jpg"),Qt.Qt.WindowStaysOnTopHint)
	#splash.showMessage("Loading %s..." % "me")
    	#splash.show()
    	#splash.raise_()  
	
	osd.show()
	#splash.raise_()
	
	#osd.trayIcon.show()
	
	app.exec_()
